//
//  sheZhiView.m
//  qmzk
//
//  Created by tangtangchitangtang on 2017/6/30.
//  Copyright © 2017年 www.fuyuan.qmzk. All rights reserved.
//

#import "sheZhiView.h"
#import "forgetViewController.h"
#import "guanYuViewController.h"
//#import "userViewController.h"
//#import "gengXinViewController.h"
@implementation sheZhiView

-(void)creatView{
    self.backgroundColor=_LightGrey;
    NSArray *nameArr=[NSArray arrayWithObjects:@"缓存清理",@"修改密码",@"关于独角兽",nil];
    for (int i=0; i<nameArr.count; i++) {
        UIView *beiJingView2=[UIView new];
        [self addSubview:beiJingView2];
        [beiJingView2 mas_makeConstraints:^(MASConstraintMaker *make) {
      
            make.top.mas_equalTo(NAV_HEIGHT+10+45*_mainHeight*i+i*1);
            make.left.right.mas_equalTo(0);
            make.height.mas_equalTo(45*_mainHeight);
        }];
        beiJingView2.backgroundColor=[UIColor whiteColor];
        beiJingView2.tag=i;
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushToZhangHu:)];
        [beiJingView2 addGestureRecognizer:tapGesture];
        
        UILabel *nameLabel=[UILabel new];
        [beiJingView2 addSubview:nameLabel];
        [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            make.left.mas_equalTo(_mainWidth*15);
        }];
     
        nameLabel.text=nameArr[i];
        nameLabel.font=[UIFont fontWithName:@"Helvetica" size:18];
        nameLabel.textColor=ColorWithRGB(50, 50, 50);
        
        UIImageView *jianTou=[UIImageView new];
        [beiJingView2 addSubview:jianTou];
        [jianTou mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-10);
            make.centerY.mas_equalTo(0);
            make.width.mas_equalTo(10);
            make.height.mas_equalTo(15);
        }];
        jianTou.image=[UIImage imageNamed:@"iconfont-fanhui-拷贝-3"];
    }
    
    UIButton *quRenBtn=[UIButton new];
    [self addSubview:quRenBtn];
    [quRenBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(NAV_HEIGHT+200);
        make.left.mas_equalTo(_mainWidth*15);
        make.right.mas_equalTo(-_mainWidth*15);
        make.height.mas_equalTo(_mainHeight*45);
    }];
    quRenBtn.backgroundColor=_backgroundColor;
    quRenBtn.layer.cornerRadius=_mainHeight*45/2;
    [quRenBtn setTitle:@"退出登录" forState:UIControlStateNormal];
    [quRenBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    quRenBtn.titleLabel.font=[UIFont fontWithName:@"Helvetica" size:16];
    [quRenBtn addTarget:self action:@selector(pushToLogin:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)pushToZhangHu:(UITapGestureRecognizer *)tap{
    if (tap.view.tag==1) {
        forgetViewController *geRen=[[forgetViewController alloc] init];
        geRen.forgetStr=@"修改密码";
        [ZQTools pushNextViewController:self.viewController andRootController:geRen];
    }

    if (tap.view.tag==0) {
        [self creatAltel];
    }
    if (tap.view.tag==2) {
        guanYuViewController *geRen=[[guanYuViewController alloc] init];
        [ZQTools pushNextViewController:self.viewController andRootController:geRen];
    }


}

- (void)clearFile
{
    NSString * cachePath = [NSSearchPathForDirectoriesInDomains (NSCachesDirectory , NSUserDomainMask , YES ) firstObject];
    NSArray * files = [[NSFileManager defaultManager ] subpathsAtPath :cachePath];
    //NSLog ( @"cachpath = %@" , cachePath);
    for ( NSString * p in files) {
        
        NSError * error = nil ;
        //获取文件全路径
        NSString * fileAbsolutePath = [cachePath stringByAppendingPathComponent :p];
        
        if ([[NSFileManager defaultManager ] fileExistsAtPath :fileAbsolutePath]) {
            [[NSFileManager defaultManager ] removeItemAtPath :fileAbsolutePath error :&error];
          
        }
    }
      [ZQTools svpInfo:@"清除缓存成功"];
    //读取缓存大小
//        float cacheSize = [self readCacheSize] *1024; 
    //    self.cacheSize.text = [NSString stringWithFormat:@"%.2fKB",cacheSize];
    
}

-(void)creatAltel{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:@"是否要清除缓存" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertController addAction:cancelAction];
    [alertController addAction: [UIAlertAction actionWithTitle: @"确定" style: UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
        [self clearFile];
    }]];
    [self.viewController presentViewController:alertController animated:YES completion:nil];
}
-(IBAction)pushToLogin:(UIButton *)tap{
 
    
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:@"是否要退出登陆" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertController addAction:cancelAction];
    [alertController addAction: [UIAlertAction actionWithTitle: @"确定" style: UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
        
        NSFileManager *defaultManager = [NSFileManager defaultManager];
        if ([defaultManager removeItemAtPath:_userModelFile error:nil]) {
            [self.viewController.navigationController popViewControllerAnimated:YES];
            //退出的时候删除别名
            [JPUSHService deleteAlias:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
                if (iResCode == 0) {
                    NSLog(@"删除别名成功");
                    [ZQTools svpInfo:@"退出登录成功"];
                   
                }
            } seq:1];
            
            
        }
        
    }]];
    [self.viewController presentViewController:alertController animated:YES completion:nil];

    
    
}


@end
